home *** CD-ROM | disk | FTP | other *** search
/ Young Minds / Young Minds Interactive CD-ROM.ISO / spacewar / objinit.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-05-31  |  2.2 KB  |  91 lines

  1. /*
  2.  * Spacewar - read in objects and set them up for the universe
  3.  *
  4.  * Copyright 1985 obo Systems, Inc.
  5.  * Copyright 1985 Dan Rosenblatt
  6.  */
  7.  
  8. #include <stdio.h>
  9. #include "spacewar.h"
  10. #include "universe.h"
  11. #include "obj.h"
  12.  
  13. VOID objinit()
  14. {
  15.     FILE *fobj;
  16.     struct obj *p=objlst;
  17.     struct universe *puniv;
  18.  
  19. #ifdef DEBUG
  20.     DBG("objinit()\n");
  21. #endif
  22.  
  23.     if (!(fobj = fopen(SWOBJ,"r"))) {
  24.         perror(SWOBJ);
  25.         exit(1);
  26.     }
  27.  
  28.     /* try to read the exact number of objects that there should be */
  29.     for (p=objlst;p < objlst+MAXOBJ;++p)
  30.         if (fscanf(fobj,"%ld\t%hd\t%c\t%d\t%hd\t%ld\t%lf\t%lf\t%lf\t%lf\n",
  31.         &p->oj_mass,&p->oj_rad,&p->oj_rep,&p->oj_octr.ip_ofst,&p->oj_oprd,
  32.         &p->oj_orad,&p->oj_ocrpt,&p->oj_optx,&p->oj_opty,&p->oj_optz) != 10) {
  33.         perror("bad object");
  34.         exit(1);
  35.         }
  36.  
  37.     if (!feof(fobj) || ferror(fobj) || fclose(fobj)) {
  38.         perror(SWOBJ);
  39.         exit(1);
  40.     }
  41.  
  42.     /* fix up orbital center and rotation matrix */
  43.     for (p=objlst+MAXOBJ;p-- > objlst;) {
  44.  
  45.         /* validate */
  46.         if (p->oj_octr.ip_ofst < 0 ||
  47.         p->oj_octr.ip_ofst >= p-objlst) {
  48.         if (p != objlst) { /* first object is center of universe */
  49.             perror("bad oj_octr");
  50.             exit(1);
  51.         }
  52.         }
  53.         switch(p->oj_rep) {
  54.         case '.':
  55.             p->oj_rep = ' ';
  56.         case '*': case 'O': case 'o': case '#':
  57.             break;
  58.         default:
  59.             perror("bad oj_rep");
  60.             exit(1);
  61.         }
  62.  
  63.         /* rotation matrix */
  64.         unity(p->oj_rmat);
  65.         xrot(p->oj_rmat,NEG(MUL(p->oj_optx,DEGTORAD)));
  66.         yrot(p->oj_rmat,NEG(MUL(p->oj_opty,DEGTORAD)));
  67.         zrot(p->oj_rmat,NEG(MUL(p->oj_optz,DEGTORAD)));
  68.  
  69.         /* place into universe */
  70.         p->oj_octr.ip_ptr = univlst + p->oj_octr.ip_ofst;
  71.         puniv = univlst + (p - objlst);
  72.         puniv->uv_type = 'O';
  73.         puniv->uv_pctr = p->oj_rep;
  74.         puniv->uv_pstn = p->oj_pstn;
  75.         puniv->uv_mass = p->oj_mass;
  76.         puniv->uv_rad = p->oj_rad;
  77.         puniv->uv_ptr.uv_obj = p;
  78.         p->oj_univ.ip_ptr = puniv;
  79.     }
  80.  
  81.     /* update objects so that universe position is current */
  82.     /* (necessary because of first-time players that crank */
  83.     /* up a new instance of the game and how they are put  */
  84.     /* into the universe the very first time)           */
  85.     updobjs();
  86.  
  87. #ifdef DEBUG
  88.     VDBG("objinit return\n");
  89. #endif
  90. }
  91.